$(function () {
  layui.use('laydate', function () {
    var laydate = layui.laydate;
    //执行一个laydate实例
    laydate.render({
      elem: '#rili' //指定元素
    });
  });
  // textarea
  tinymce.init({
    selector: '#tinydemo', //容器，可使用css选择器
    language: 'zh_CN', //调用放在langs文件夹内的语言包
  });
  // 下拉框获取文章类别
  $.ajax({
    url: '/admin/category/list',
    success: res => {
      // console.log(res)
      let HtmlStr = template('temp_list', res)
      $('.category').html(HtmlStr)
    }
  })
  // 根据id获取文章信息
  let id = decodeURI(window.location.search.split('=')[1]).split("'")[1]
  // let search = window.location.search
  // let idObj = new SearchParames(search)
  // let id = idObj.get("id")
  console.log(id);
  let state
  $.ajax({
    url: '/admin/article/search',
    data: { id },
    success: res => {
      if (res.code == 200) {
        state = res.data.state
        $('.title').val(res.data.title)
        $('#tinydemo').text(res.data.content)
        $('[name="date"]').val(res.data.date)
        $('.category').val(res.data.categoryId)
        $('.article_cover').attr('src', res.data.cover)
      }
    }
  })
  // 给修改按钮添加点击事件
  $('.btn-edit').on('click', function (e) {
    e.preventDefault()
    let fd = new FormData(document.querySelector('form'))
    let content = tinyMCE.editors['tinydemo'].getContent()
    fd.append("content", content)
    fd.append('id', id)
    fd.append('state', state)
    $.ajax({
      type: 'post',
      url: '/admin/article/edit',
      data: fd,
      contentType: false,
      processData: false,
      success: res => {
        if (res.code == 200) {
          // console.log(res)
          layer.msg(res.msg, { time: 1000 })
          setTimeout(function () {
            if (res.code == 200) {
              window.location.href = './article_list.html'
            }
          }, 2000)
        }
      
    }
    })
})
})